// add by yanyj 20180724 start
// 电子地图的主要方法

let pageMap = null;


class PageMap {
    constructor() {
        this.map = null;
        this.map_ = null;
        this.clickFeatureEvents = {}

    }
    init(mapSetting) {

        let map = new olMap();
        let layers = createTianDiLayer();

        // mouse point control
        let mousePositionControl = new ol.control.MousePosition({
            coordinateFormat: ol.coordinate.createStringXY(4),
            projection: 'EPSG:4326',
            // comment the following two lines to have the mouse position
            // be placed within the map.
            className: 'custom-mouse-position',
            target: document.getElementById('mouse-position'),
            undefinedHTML: '&nbsp;'
        });

        map.setMap(new ol.Map({
            target: 'dzdtMap',

            // layers:  [tiandiLayers.base, tiandiLayers.note],
            //生产环境注释
            layers: layers,
            projection: 'EPSG:4326',
            // displayProjection: _ol.proj.EPSG4326,
            renderer: 'canvas',
            projectStage: 'product',
            minZoom: 17,
            loadTilesWhileAnimating: true,
            extent: transform([-180, -90, 180, 90]),
            view: new ol.View({
                center: ol.proj.fromLonLat([+mapSetting.center[0], +mapSetting.center[1]]),
                zoom:  +mapSetting.zoom,
                maxZoom: 18
            }),
            controls: ol.control.defaults({
                attributionOptions: {
                    collapsible: false
                }
            }).extend([
                new ol.control.Zoom(),
                new ol.control.Rotate(),
                mousePositionControl
                // new _ol.control.MousePosition({ projection: _ol.proj.EPSG4326}),
            ]),
        }), layers);

        this.map = map;
        this.map_ = map.getMap();

        // 启动点击事件
        this.activeClickFeature();
        return this;
    }

    activeClickFeature() {
        let events = this.clickFeatureEvents;

        this.map.activateSelect('click', function (feature, coordinate) {
            let type = feature.type;

            events[type].call(undefined, feature, coordinate);

        });

        this.map.activateChangeMouseStyleOnFeature();
    }

    /**
     *  装载图层
     * @param {BaseLayer} layer 地图
     */
    addLayer(layer) {
        // 判断是否是BaseLayer
        if(layer instanceof BaseLayer) {
            layer.setMap(this.map);
            layer.init();
            this.map.addLayer(layer.getName(), layer.getLayer());

        } else {
            throw new Error(`${layer.name}非法！`);
        }

        // 填充layer的feature点击事件
        if(layer.featureType && layer.getClickFeatureEvent) {
            this.clickFeatureEvents[layer.featureType] = layer.getClickFeatureEvent();
        }

        return this;
    }

    // 手动注入feature的点击事件
    setEventOnClickFeature(type, eventFunction) {
        this.clickFeatureEvents[type] = eventFunction;
        return this;
    }

    /**
     * 获取封装
     */
    getMap() {
        return this.map;
    }
    getMap_() {
        return this.map_;
    }

}
// add by yanyj 20180724 end